import { BaseOpt } from "./base";
import { ContentModel } from "../models";

export default class DeleteOpt extends BaseOpt{
  
  private index = 0
  private length = 0

  private _deleted = ""
  
  constructor(index:number,length:number){
    super()
    this.index = index
    this.length = length
    this.type = 'DeleteOpt'
  }

  apply(): void {
    this._deleted = this._model.delete(this.index,this.length)
    if(this._selection){
      this._selection.update(this.index, 0)
    }
  }

  reverse(): void {
    this._model.insert(this.index,this._deleted)
    if(this._selection){
      this._selection.update(this.index + this._deleted.length, 0)
    }
  }

  merge(opt: BaseOpt): boolean {
    if(opt.type!==this.type) return false
    if(this.isTimeout()) return false
    let delOpt = opt as DeleteOpt
    if(delOpt.index + delOpt.length !== this.index){
      return false
    }
    this.index = delOpt.index
    this.length += delOpt.length
    return true
  }
}